﻿using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.Utility.Castle;
using BEGDC.Utility.ExceptionManagement;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.CCFlow;
using BEGDC.CC.Web.View;
using System.IO;

namespace BEGDC.CC.Web.Workflow
{
    /// <summary>
    /// Summary description for AttachmentExport.
    /// </summary>
    public class AttachmentExport : System.Web.UI.Page
    {

        private static object SyncObj = new object();

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            string strFileid = Request.QueryString["FileID"];
            string fileType = Request.QueryString["Type"];
            int iFileid = 0;
            string err = string.Empty;
            DataTable table;
            if (strFileid != null && strFileid != string.Empty)
            {
                iFileid = Convert.ToInt32(strFileid);
                IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
                if (fileType == "Attachment")
                    table = queryService.GetAttachmentByFileId(iFileid);
                else
                    table = queryService.GetTemplateByID(iFileid);

                if (table != null && table.Rows.Count > 0)
                    OutPutFile(table.Rows[0]["FilePath"].ToString(), table.Rows[0]["FileName"].ToString(), out err);
            }
        }


        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

        private void OutPutFile(string sourceFile, string fileName, out string errMsg)
        {
            //lock (AttachmentExport.SyncObj)
            //{
            //            int tryTime = 0;

            var buffer = File.ReadAllBytes(sourceFile);

            errMsg = "";
            try
            {
                Response.Buffer = true;

                Response.ContentType = "application/octet-stream";
                //中文文件名需要UTF8编码
                Response.AddHeader("Content-Disposition", "attachment; filename=\"" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + "\"");
                Response.AddHeader("Content-Length", buffer.LongLength.ToString());


                Response.BinaryWrite(buffer);
                Response.Flush();
                Response.End();
            }
            catch (Exception exp)
            {
                errMsg = exp.Message;
            }

            //}
        }
    }
}
